package com.gitee.wsl.compose.mvi.ui.floatingactionbutton

import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.FloatingActionButtonDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.gitee.wsl.compose.mvi.component.InteractionSourceProjector
import com.gitee.wsl.compose.mvi.ui.icon.IconProjector
import com.gitee.wsl.compose.mvi.ui.text.TextProjector

class FloatingActionButtonProjector(
    interactor: FloatingActionButtonInteractor,
    presenter: FloatingActionButtonPresenter = FloatingActionButtonPresenter.Default
) : InteractionSourceProjector<FloatingActionButtonInteractor, FloatingActionButtonPresenter>(interactor, presenter) {

    @Composable
    override fun body(
        combinedModifier: Modifier,
        interactor: FloatingActionButtonInteractor,
        presenter: FloatingActionButtonPresenter,
        interactionSource: MutableInteractionSource,
    ) {
        val textProjector = interactor.text?.let { TextProjector(it, presenter.text) }
        val iconProjector = interactor.icon?.let { IconProjector(it, presenter.icon) }

        if (textProjector == null) {
            FloatingActionButton(
                onClick = interactor.onClick,
                modifier = combinedModifier,
                interactionSource = interactionSource,
                shape = presenter.shape,
                containerColor = presenter.backgroundColor,
                contentColor = presenter.contentColor,
                elevation = FloatingActionButtonDefaults.elevation()
            ) {
                iconProjector?.invoke()
            }
        } else {
            ExtendedFloatingActionButton(
                text = { textProjector() },
                onClick = interactor.onClick,
                modifier = combinedModifier,
                icon = {iconProjector?.invoke() },
                interactionSource = interactionSource,
                shape = presenter.shape,
                containerColor = presenter.backgroundColor,
                contentColor = presenter.contentColor,
                elevation = FloatingActionButtonDefaults.elevation()
            )
        }
    }
}